package cn.crce.cloud9.simple;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * @author OnCloud9
 * @description
 * @project NRCE-TEST
 * @date 2022年09月10日 上午 10:40
 */
public class Case38 implements ActionListener {
    private JFrame frame;
    private JButton button, saveButton;
    private JTextArea textArea;
    private JFileChooser dia;
    private JPanel buttonPanel;

    public void init() {
        frame = new JFrame("file chooser");
        dia = new JFileChooser();
        button = new JButton("open file");
        button.setActionCommand("open");
        //*********Found**********
        button.addActionListener(this);

        saveButton = new JButton("save file");
        //*********Found**********
        saveButton.addActionListener(this);

        buttonPanel = new JPanel();
        buttonPanel.add(button);
        buttonPanel.add(saveButton);

        //*********Found**********
        textArea = new JTextArea("", 10, 10);
        //*********Found**********
        frame.getContentPane().add(buttonPanel, BorderLayout.NORTH);
        frame.getContentPane().add(textArea, BorderLayout.CENTER);

        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    //*********Found**********
    public void actionPerformed(ActionEvent event) {
        if (event.getActionCommand().equals("open"))
            dia.showOpenDialog(frame);
        else
            dia.showSaveDialog(frame);

        dia.setVisible(true);
        File file = dia.getSelectedFile();
        if (file != null) {
            String fileName = file.getAbsolutePath();
            textArea.append("path of selected file: " + fileName + "\r\n");
        }
    }

    public static void main(String args[]) {
        Case38 example = new Case38();
        //*********Found**********
        example.init();
    }
}
